#include <func.h>
// 定义一个消息结构体
typedef struct myMsg
{
    long mtype;
    char metext[512];
} myMsg_t;

// 常量；
int msqid;
int fdw;
int fdr;
// 函数

void sigFunc(int signum);
int chatB();
int sndToB1(int msqid, char *msg);
void closeAll();
int main()
{
    //==============B=='操作'====================//
    // 跟A前面一，只是他需要创建一个消息队列；
    //    shmid = shmget(1001,4096,IPC_CREAT|0600);
    //   ERROR_CHECK(shmid,-1,"shmid");
    //   pid = (char*)shmat(1001,NULL,0);
    //   ERROR_CHECK(pid,(void*)-1,"shmat");
    // 创建信号
    //   semid = semget(2000,1,IPC_CREAT|0600);
    //   ERROR_CHECK(semid,-1,"semget");
    //----------------------------------------------------------------
    // 创建消息队列
    msqid = msgget(2000, IPC_CREAT | 0600);
    ERROR_CHECK(msqid, -1, "msgget");

    signal(SIGINT, sigFunc);
    signal(SIGQUIT, sigFunc);

    // 开始建立通讯
    int ret = chatB();
    ERROR_CHECK(ret, -1, "chatB");

    return 0;
}
int chatB()
{
    // 开始与a进行通信,设置监听,打开通道
    fdw = open("/home/rui/warehouse/1精匠/四窗口聊天/pipe1", O_WRONLY);
    ERROR_CHECK(fdw, -1, "fdw open");
    fdr = open("/home/rui/warehouse/1精匠/四窗口聊天/pipe2", O_RDONLY);
    ERROR_CHECK(fdr, -1, "fdr open");
    fd_set rdset;
    // 设置超时
    struct timeval timeOut;
    timeOut.tv_sec = 5000;
    timeOut.tv_usec = 1500000;
    char buf[4096] = {0};
    while (1)
    {

        FD_ZERO(&rdset);
        FD_SET(fdr, &rdset);
        FD_SET(STDIN_FILENO, &rdset);
        int ret = select(fdr + 1, &rdset, NULL, NULL, &timeOut);
        if (ret == 0)
        {
            puts("time out!");
        }
        if (FD_ISSET(fdr, &rdset))
        {
            memset(buf, 0, sizeof(buf));
            int ret = read(fdr, buf, sizeof(buf));
            if (ret == 0)
            {
                sigFunc(2);
                break;
            }
            // 获取从A来的数据类型

            int type;
            sscanf(buf, "%*d %d", &type);
            int snd_ret = 0; // 实际发送的字节数
            if (type == 2)
            {
                sigFunc(2);
                exit(0);
            }
            snd_ret = sndToB1(msqid, buf);
            ERROR_CHECK(snd_ret, -1, "B sndToB1");
        }

        if (FD_ISSET(STDIN_FILENO, &rdset))
        {
            time_t localtm;
            time(&localtm);
            localtm += 8 * 3600;
            memset(buf,0,sizeof(buf));
            // 传送数据,读取标准输入区的数据，标记为3号信息，发送给A和B;
            int ret = read(STDERR_FILENO, buf, sizeof(buf));
            if (ret == 0)
            {
                puts("I Quit");
                break;
            }
             char sstoA[1024] = {0};
            sprintf(sstoA, "%ld %d %s", localtm, 1, buf);
            write(fdw, sstoA, sizeof(sstoA));
            char sstoB1[1024] = {0};
            sprintf(sstoB1, "%ld %d %s", localtm, 3, buf);
            sndToB1(msqid, sstoB1); // 发送给b1;
           
        }
    }
    close(fdr);
    close(fdw);
    return 1;
}

int sndToB1(int msqid, char *msg)
{
    // 使用消息队列发送信息；
    myMsg_t msgtoB1;   // 创建一个消息结构体
    msgtoB1.mtype = 1; 
    // 消息队列发送信息；
    memset(msgtoB1.metext, 0, sizeof(msgtoB1.metext));
    memcpy(msgtoB1.metext,msg,strlen(msg));
    msgsnd(msqid, &msgtoB1, strlen(msg), 0);
    return 1;
}

void sigFunc(int signum)
{
    printf("准备退出\n");
    sndToB1(msqid, "0 2 0");
    write(fdw, "0 2 0", 5);
    usleep(500);
    closeAll();
}

void closeAll()
{
    msgctl(msqid, IPC_RMID, 0);
close(fdw);
close(fdr);
    exit(0);
}
